package com.leyou.controller;

import com.leyou.search.SearchRequest;
import com.leyou.search.SearchResult;
import com.leyou.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@CrossOrigin
@RestController
public class SearchController {

    @Autowired
    private SearchService searchService;

    /**
     * 搜索商品
     * @return
     */
    @RequestMapping("page")
    public ResponseEntity<SearchResult> search(@RequestBody SearchRequest request) {
        System.out.println("搜索数据是否进来了：" + request.getKey()+ " " +
                request.getPage()+ " " );
        SearchResult result = this.searchService.search(request);
        if (result == null || CollectionUtils.isEmpty(result.getItems())) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }

        System.out.println("最终数据是：" + result.getItems().get(0).toString());
        return ResponseEntity.ok(result);
    }


}

